package com.wangjiyuan.shiwupai.ui_food.food_class.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.support.v4.graphics.drawable.RoundedBitmapDrawable;
import android.support.v4.graphics.drawable.RoundedBitmapDrawableFactory;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.target.BitmapImageViewTarget;
import com.wangjiyuan.shiwupai.R;
import com.wangjiyuan.shiwupai.base.BaseApplication;
import com.wangjiyuan.shiwupai.bean.first_food.FoodClassBean;
import com.wangjiyuan.shiwupai.utils.RecyclerViewAdapterHelper;

import java.util.List;

/**
 * Created by QXL on 2016/11/30.
 */

public class FoodClassRecAdapter extends RecyclerViewAdapterHelper<FoodClassBean.FoodsBean> {
    public FoodClassRecAdapter(Context context, List<FoodClassBean.FoodsBean> list) {
        super(context, list);
    }

    @Override
    public RecyclerView.ViewHolder onCreateMyViewHolder(ViewGroup parent, int viewType) {
        View view = mInflater.inflate(R.layout.item_foodclass, parent,false);
        return new ViewHolderFoodClass(view);
    }

    @Override
    public void onBindMyViewHolder(final RecyclerView.ViewHolder holder, int position) {
        FoodClassBean.FoodsBean foodsBean = mList.get(position);
        int healthLight = foodsBean.getHealthLight();
        Glide.with(mContext)
                .load(foodsBean.getThumbImageUrl())
                .asBitmap()
                .placeholder(R.drawable.img_default_food_thumbnail)
                .into(new BitmapImageViewTarget(((ViewHolderFoodClass)holder).imageView_item_foodClass){
                    @Override
                    protected void setResource(Bitmap resource) {
                        RoundedBitmapDrawable circularBitmapDrawable =
                                RoundedBitmapDrawableFactory.create(mContext.getResources(), resource);
                        circularBitmapDrawable.setCircular(true);
                        ((ViewHolderFoodClass)holder).imageView_item_foodClass.setImageDrawable(circularBitmapDrawable);
                    }
                });
        ((ViewHolderFoodClass)holder).textView_item_foodClass_name.setText(foodsBean.getName());
        ((ViewHolderFoodClass) holder).textView_item_foodClass_num.setText(foodsBean.getCalory());
        ((ViewHolderFoodClass) holder).textView_item_foodClass_numOther.setText("千卡/"+foodsBean.getWeight()+"克");
        switch (healthLight){
            case 1:
                ((ViewHolderFoodClass) holder).imageView_item_foodClass_light.setImageResource(R.drawable.ic_food_light_green);
                break;
            case 2:
                ((ViewHolderFoodClass) holder).imageView_item_foodClass_light.setImageResource(R.drawable.ic_food_light_yellow);
                break;
            case 3:
                ((ViewHolderFoodClass) holder).imageView_item_foodClass_light.setImageResource(R.drawable.ic_food_light_red);
                break;
        }
    }

    class ViewHolderFoodClass extends RecyclerView.ViewHolder {
        private ImageView imageView_item_foodClass,imageView_item_foodClass_light;
        private TextView textView_item_foodClass_name,textView_item_foodClass_num,textView_item_foodClass_numOther;
        public ViewHolderFoodClass(View itemView) {
            super(itemView);
            imageView_item_foodClass= (ImageView) itemView.findViewById(R.id.imageView_item_foodClass);
            RelativeLayout.LayoutParams layoutParams=new RelativeLayout.LayoutParams(BaseApplication.width/5,BaseApplication.width/5);
            imageView_item_foodClass.setLayoutParams(layoutParams);

            imageView_item_foodClass_light= (ImageView) itemView.findViewById(R.id.imageView_item_foodClass_light);

            textView_item_foodClass_name= (TextView) itemView.findViewById(R.id.textView_item_foodClass_name);
            textView_item_foodClass_num= (TextView) itemView.findViewById(R.id.textView_item_foodClass_num);
            textView_item_foodClass_num.setHeight(BaseApplication.width/10);
            textView_item_foodClass_numOther= (TextView) itemView.findViewById(R.id.textView_item_foodClass_numOther);
            textView_item_foodClass_numOther.setHeight(BaseApplication.width/10);
        }
    }
}
